package com.whz.workmanageol.service.impl;

import com.whz.workmanageol.dao.StudentMapper;
import com.whz.workmanageol.dao.WorkMapper;
import com.whz.workmanageol.entity.Student;
import com.whz.workmanageol.entity.Work;
import com.whz.workmanageol.service.WorkService;
import com.whz.workmanageol.utils.UUIDGenerator;
import com.whz.workmanageol.vo.FilePath;
import com.whz.workmanageol.vo.StudentWorkVO;
import com.whz.workmanageol.vo.StudentWorkVOComparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
@Transactional
public class WorkServiceImpl implements WorkService {
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private StudentMapper studentMapper;
    /**
     * 教师发布作业时创建作业文件
     * @param filePath
     */
    @Override
    public void saveWork(FilePath filePath, String publishid, List<Student> studentList) {
        for(Student student : studentList){
            Work work = new Work();
            work.setId(UUIDGenerator.getUUID());
            work.setPublishid(publishid);
            work.setStudentid(student.getId());
            work.setFilepath(filePath.toString());
            work.setFilename(student.getStudentid()+"_"+student.getUsername()+"_"+filePath.getSeq());
            work.setSuffix(".doc");
            work.setCreatedate(new Date());
            workMapper.insert(work);
        }
    }

    /**
     * 通过publishid确定学生和作业
     * @param publishid
     * @return
     */
    @Override
    public List<StudentWorkVO> studentWorkList(String publishid) {
        List<StudentWorkVO> studentWorkVOList = new ArrayList<>();

        Example example = new Example(Work.class);
        example.createCriteria().andEqualTo("publishid", publishid);
        List<Work> workList = workMapper.selectByExample(example);

        for(Work wor : workList){
            Student theStu = studentMapper.selectByPrimaryKey(wor.getStudentid());
            StudentWorkVO studentWorkVO = new StudentWorkVO(theStu.getStudentid(), theStu.getUsername(), wor);
            studentWorkVOList.add(studentWorkVO);
        }
        Collections.sort(studentWorkVOList, new StudentWorkVOComparator());
        return studentWorkVOList;
    }

    @Override
    public Work findWorkById(String workid) {
        return workMapper.selectByPrimaryKey(workid);
    }

    /**
     * 设置作业的批阅时间
     * @param workid
     */
    @Override
    public void readMark(String workid) {
        Work work = workMapper.selectByPrimaryKey(workid);
        work.setReaddate(new Date());
        workMapper.updateByPrimaryKey(work);
    }

    @Override
    public void saveScore(String workid, String scoreNum) {
        Work work = workMapper.selectByPrimaryKey(workid);
        work.setScore(scoreNum);
        work.setReaddate(new Date());
        workMapper.updateByPrimaryKey(work);
    }

}
